<template>
    <div>
        <div>
            <el-form :inline="true" :model="formInline" class="demo-form-inline" style="height: 80%;">
                <el-form-item label="转种日期:">
                    <el-date-picker v-model="formInline.sdate" type="date" aria-label="选择日期" style="width: 200px" />
                    <el-date-picker v-model="formInline.edate" type="date" aria-label="选择日期" style="width: 200px" />
                </el-form-item>

                <el-form-item label="后裔耳号:">
                    <el-input v-model="formInline.earbugles">
                    </el-input>
                </el-form-item>

            </el-form>
            <div>
                <el-button type="danger" size="large" @click="handleAdd" :icon="Plus">新增</el-button>
                <el-button type="danger" size="large" @click="handleDeleteAll" :icon="Delete">删除</el-button>
            </div>
        </div>
        <div v-if="TagBool">
            <el-tag style="width:1920px;color: black;height: 50px;" type="danger" :iocn="Delete">
                <spna style="font-size: 15px">
                    <Warning style="width: 1em; height: 1em; margin-right: 8px" color="red" />
                    <span style="margin-right: 1800px">已选择 {{ TagCount }} 项</span>
                </spna>
            </el-tag>
        </div>
        <div v-else>
            <el-tag style="width:1920px;color: black;height: 50px;" type="success" :iocn="Delete">
                <spna style="font-size: 15px">
                    <SuccessFilled style="width: 1em; height: 1em; margin-right: 8px" color="green" />
                    <span style="margin-right: 1800px">已选择 {{ TagCount }} 项</span>
                </spna>
            </el-tag>
        </div> <br />
        <el-table ref="multipleTableRef" :data="PurList" style="width: 100%" border
            @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" />
            <el-table-column label="序号" width="120" fixed>
                <template v-slot="scope">
                    <span>{{ scope.$index + (page.pageIndex - 1) * page.pageSize + 1 }}</span>
                </template>
            </el-table-column>
            <el-table-column property="ConversionDate" label="转种日期" width="120px">
                <template v-slot="scope">
                    <span>{{ scope.row.ConversionDate ? scope.row.ConversionDate.substring(0, 10) :
                        null
                        }}</span>
                </template>
            </el-table-column>
            <el-table-column property="RaisedSheep" label="育成羊耳号" />
            <el-table-column property="ShiftCottage" label="转入栋舍" width="200px">
            </el-table-column>
            <el-table-column property="ShiftField" label="转入栏位" width="120px">
            </el-table-column>
            <el-table-column property="AtPresentCottage" label="当前栋舍" width="120px">
            </el-table-column>
            <el-table-column property="AtPresentField" label="当前栏位" width="120px">
            </el-table-column>
            <el-table-column property="Sex" label="性别" width="120px">
                <template v-slot="scope">
                    <span>{{ scope.row.Sex ? "母" : "公"
                        }}</span>
                </template>
            </el-table-column>
            <el-table-column property="TransplantingAge" label="转种日龄" width="120px">
            </el-table-column>
            <el-table-column property="CreateBy" label="创建人" width="120px">
            </el-table-column>
            <el-table-column property="CreateDate" label="创建时间" width="200px">
                <template v-slot="scope">
                    <span>{{ scope.row.CreateDate ? scope.row.CreateDate.replace('T', ' ').substring(0, 19) : null
                        }}</span>
                </template>
            </el-table-column>
            <el-table-column property="UpdateBy" label="修改人" width="120px">
            </el-table-column>
            <el-table-column property="UpdateDate" label="修改时间" width="200px">
                <template v-slot="scope">
                    <span>{{ scope.row.UpdateDate ? scope.row.UpdateDate.replace('T', ' ').substring(0, 19) : null
                        }}</span>
                </template>
            </el-table-column>
            <el-table-column property="ExamineBy" label="审核人" width="120px">
            </el-table-column>
            <el-table-column property="ExamineDate" label="审核时间" width="200px">
                <template v-slot="scope">
                    <span>{{ scope.row.ExamineDate ? scope.row.ExamineDate.replace('T', ' ').substring(0, 19) : null
                        }}</span>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="120">
                <template v-slot="scope">
                    <el-button link type="primary" size="default" @click="handleEdit(scope.row)">
                        编辑
                    </el-button>
                    <el-button link type="primary" size="default"
                        @click="handleDelete(scope.row.SubcultivationId)">删除</el-button>


                </template>
            </el-table-column>
        </el-table>
        <table style="text-align: center;">
            <tr>
                <td>
                    <div>
                        <el-pagination v-model:current-page="page.pageIndex" v-model:page-size="page.pageSize"
                            :page-sizes="[1, 15, 30, 45]" :background="true"
                            layout="slot,total, sizes, prev, pager, next, jumper" :total="page.totalCount"
                            @size-change="handleSizeChange" @current-change="handleCurrentChange" />
                    </div>
                </td>
            </tr>
        </table>
        <el-dialog v-model="dialogTableVisible" title="后裔转钟-修改" style="width: 85%; height: 80%;"
            :before-close="NewUpdate">
            <!-- {{ purchaseForm }} -->
            <div>
                <el-button type="danger" size="large" @click="AddPurInfo" :icon="SuccessFilled">保存</el-button>
                <br>
            </div>
            <el-form :inline="true" :model="purchaseForm" class="demo-form-inline" style="height: 80%;">

                <el-form-item label="转钟日期:">
                    <el-date-picker v-model="purchaseForm.ConversionDate" type="date" aria-label="选择日期"
                        style="width: 200px" value-format="YYYY-MM-DD" />
                </el-form-item>

            </el-form>
            <el-table :data="rowList" style="width: 100%;" border :scrollbar-always-on="true">
                <el-table-column label="序号" fixed>
                    <template v-slot="scope">
                        <span>{{ scope.$index + (page.pageIndex - 1) * page.pageSize + 1 }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="育成羊耳号" prop="Earbugles">
                    <el-select v-model="purchaseForm.RaisedSheep" style="width: 200px;" @change="ChangData">
                        <el-option v-for="item in HYList" :key="item.id" :label="item.earNumber"
                            :value="item.earNumber" />
                    </el-select>

                </el-table-column>
                <el-table-column label="转入栋舍" prop="CottageName">
                    <el-select v-model="purchaseForm.ShiftCottage" style="width: 200px;" @change="GetField">
                        <el-option v-for="item in Cottage" :key="item.Id" :label="item.Name" :value="item.Name" />
                    </el-select>
                </el-table-column>
                <el-table-column label="转入栏位" prop="FieldName">
                    <span>
                        <el-select v-model="purchaseForm.ShiftField" style="width: 200px;">
                            <el-option v-for="item in Field" :key="item.Id" :label="item.Name" :value="item.Name" />
                        </el-select>
                    </span>
                </el-table-column>
                <el-table-column label="栋舍" prop="CottageName">
                    {{ purchaseForm.AtPresentCottage }}
                </el-table-column>
                <el-table-column label="栏位" prop="FieldName">
                    <span>
                        {{ purchaseForm.AtPresentField }}
                    </span>
                </el-table-column>
                <el-table-column label="性别" prop="PurSex">
                    {{ purchaseForm.Sex ? "公" : "母" }}
                </el-table-column>
                <el-table-column label="转钟日龄" prop="GoodName">
                    <span>
                        <el-input v-model="purchaseForm.TransplantingAge" style="width: 250px;"></el-input>
                    </span>
                </el-table-column>
            </el-table>
        </el-dialog>
    </div>
</template>
<script setup lang="ts">
import http from '@/http';
import { ref, reactive, inject, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { AxiosStatic } from 'axios';
import {
    Plus,
    Search,
    Delete,
    DocumentChecked,
    Back,
    Warning,
    SuccessFilled,
    Stamp
} from '@element-plus/icons-vue'
import { useRoute, useRouter } from "vue-router"
const Router = useRouter();
//新增弹出层
const dialogTableVisible = ref(false);
//查询条件
const formInline = reactive({

    sdate: "",
    edate: "",
    earbugles: "",

});
//采购添加表单
const purchaseForm = reactive({
    SubcultivationId: "",//转种编号
    RaisedSheep: "",//育成羊耳号
    ShiftCottage: "",//转入栋舍
    ShiftField: "",//转入栏位
    AtPresentCottage: "",//当前栋舍
    AtPresentField: "",//当前栏位
    Sex: true,//性别
    TransplantingAge: "",//转钟日
    ConversionDate: ""//转钟日期
})
//新增行列表
const rowList = ref([]);

const newRow = reactive({
    SubcultivationId: "",//转种编号
    RaisedSheep: "",//育成羊耳号
    ShiftCottage: "",//转入栋舍
    ShiftField: "",//转入栏位
    AtPresentCottage: "",//当前栋舍
    AtPresentField: "",//当前栏位
    Sex: true,//性别
    TransplantingAge: "",//转钟日
    ConversionDate: ""//转钟日期
});
//新增行
const addFormInfo = () => {
    purchaseForm.SubcultivationId = "";
    purchaseForm.RaisedSheep = "";
    purchaseForm.ShiftCottage = "";
    purchaseForm.ShiftField = "";
    purchaseForm.AtPresentCottage = "";
    purchaseForm.AtPresentField = "";
    purchaseForm.Sex = true;
    purchaseForm.TransplantingAge = "";
    purchaseForm.ConversionDate = "";
    rowList.value.push(newRow);
}
//羊只类型集合
const sheeptype = ref([]);
//获取羊只类型
const Getsheeptype = () => {
    http.get("/routechen/api/ReadPurchase/HandleSheepType").then(res => {
        sheeptype.value = res.data;
    });
}
//仓库集合
const StashInfo = ref([]);
//仓库类型
const GetStashInfo = () => {
    http.get("/routechen/api/ReadPurchase/HandleStash").then(res => {
        StashInfo.value = res.data;
    });
}



//栋舍集合
const Cottage = ref([]);
//栋舍下拉方法
const GetCooter = () => {

    http.get("OrganizationRead/api/Cottage/HandleGetCot").then(res => {
        Cottage.value = res.data;
    })
}
//栏位集合
const Field = ref([]);
//栏位下拉方法
const GetField = () => {
    http.get("OrganizationRead/api/Field/HandleGetSheepFarm?Id=" + purchaseForm.ShiftCottage).then(res => {
        Field.value = res.data;
    })
}


//栏位集合
const Field1 = ref([]);
//栏位下拉方法
const GetField1 = () => {
    http.get("OrganizationRead/api/Field/HandleGetSheepFarm?Id=" + purchaseForm.AtPresentCottage).then(res => {
        Field1.value = res.data;
    })
}


//采购员集合
const StaffList = ref([]);
//采购员下拉方法
const GetStaff = () => {
    http.get("Readroute/api/StaffInfo/GetStaff").then(res => {
        StaffList.value = res.data;
    })
}
//核算单元集合
const AccountingList = ref([]);
//核算单元方法
const GetAccounting = () => {
    http.get("routechen/api/ReadPurchase/HandleAccounting").then(res => {
        AccountingList.value = res.data;
    })
}
//核算单元集合
const DigestList = ref([]);
//核算单元方法
const GetDigest = () => {
    http.get("routechen/api/ReadPurchase/HandleDigest").then(res => {
        DigestList.value = res.data;
    })
}
//新增行弹出层
const handleAdd = () => {
    Router.push("ShowSubcultivation");
}
//个体采购信息显示
const page = reactive({
    pageIndex: 1,
    pageSize: 15,
    totalCount: 0,
    pageCount: 0
});

const PurList = ref<{}[]>([]);
//个体采购显示
const handleShowPurchase = () => {
    http.get("OrganizationRead/api/Subcultivation/GetSubcultivation", {
        params: {
            Index: page.pageIndex,
            Size: page.pageSize,
            SDtate: formInline.sdate,
            EDtate: formInline.edate,
            earNumber: formInline.earbugles,
        }
    }).then(res => {
        PurList.value = res.data.datas;
        page.totalCount = res.data.totalCount;
        page.pageCount = res.data.pageCount;
    });
}
const sheepSelList = ref<{
    PurId: any,
    DocumentNumber: any,
    PurDate: any,
    AccountingUnit: any,
    SheepTypeInfo: any,
    PruTytpe: any,
    Supplier: any,
    Digest: any,
    Earbugles: any,
    CottageName: any,
    FieldName: any,
    PurSex: any,
    Phase: any,
    Buyer: any,
    Stash: any,
    GoodName: any,
    Package: any,
    MeasuringUnit: any,
    QuantityNumber: any,
    AveragePrice: any,
    PriceDiscount: any,
    PriceAdjust: any,
    TotalWeight: any,
}[]>([]);
const TagBool = ref(true);
const TagCount = ref(0);
let ids = "";
//获取选中的数据
const handleSelectionChange = (val: PurList | any) => {
    sheepSelList.value = val;
    ids = "";
    val.forEach(item => {
        ids += item.SubcultivationId + ",";
    });
    ids = ids.substring(0, ids.length - 1);
    TagCount.value = ids.split(",").length;
    if (ids == "") {
        TagCount.value = 0;
    }
    if (TagCount.value == 0) {
        TagBool.value = true
    }
    else {
        TagBool.value = false
    }
}
//每页显示条数
const handleSizeChange = (val: number) => {
    page.pageSize = val;
    handleShowPurchase();
}
//当前页索引
const handleCurrentChange = (val: number) => {
    page.pageIndex = val;
    handleShowPurchase();
}
//羊只采购添加
const AddPurInfo = () => {

    http.post("Organization/api/Subcultivation/UpSubcultivation", purchaseForm).then(res => {
        if (res.data > 0) {
            ElMessage({
                type: 'success',
                message: '修改成功!',
                duration: 2000
            })
            if (HYEHID.value != purchaseForm.RaisedSheep) {

                UppType(HYEHID.value);
            }
            UppType(purchaseForm.RaisedSheep);
            handleShowPurchase();
            NewUpdate();
            dialogTableVisible.value = false;
        }
        else if (res.data == -1) {
            ElMessage({
                type: 'error',
                message: '修改失败，耳号不能重复!',
                duration: 2000
            })
            return;
        }
        else {
            ElMessage({
                type: 'error',
                message: '修改失败!',
                duration: 2000
            })
            return;
        }
    })


}

//批量删除
const handleDeleteAll = () => {
    ElMessageBox.confirm(
        '确定要删除当前数据吗?',
        '警告',
        {
            confirmButtonText: '是',
            cancelButtonText: '否',
            type: 'warning',
        }
    ).then(() => {
        http.get("Organization/api/Subcultivation/leAllDeletSubcultivation?Ids=" + ids).then(res => {
            if (res.data > 0) {
                ElMessage({
                    type: "success",
                    message: "删除成功",
                    duration: 2000
                });
                ids.split(",").forEach(x => {
                    UppType(x);
                })
                handleShowPurchase();
            }
            else {
                ElMessage({
                    type: "error",
                    message: "删除失败",
                    duration: 2000
                });
                handleShowPurchase();
            }
        })
    })
}


//批量审核
const handleExamine = () => {
    ElMessageBox.confirm(
        '确定要审核通过当前数据吗?',
        '警告',
        {
            confirmButtonText: '是',
            cancelButtonText: '否',
            type: 'warning',
        }
    ).then(() => {
        http.put("webroute/api/EntityPurchase/HandleEmaminePurchase?purId=" + sheepSelList.value.map((item) => item.PurId).toString()).then(res => {
            if (res.data > 0) {
                ElMessage({
                    type: "success",
                    message: "审核通过",
                    duration: 2000
                })
                handleShowPurchase();
            }
            else if (res.data == -1) {
                ElMessage({
                    type: "error",
                    message: "当前数据当中已有审核通过的数据",
                    duration: 2000
                })
                handleShowPurchase();
            }
            else {
                ElMessage({
                    type: "error",
                    message: "审核失败",
                    duration: 2000
                })
                handleShowPurchase();
            }
        })
    })
}
//批量重新审核
const handleNewExamine = () => {
    ElMessageBox.confirm(
        '确定要审核通过当前数据吗?',
        '警告',
        {
            confirmButtonText: '是',
            cancelButtonText: '否',
            type: 'warning',
        }
    ).then(() => {
        http.put("webroute/api/EntityPurchase/HandleNewEmaminePurchase?purId=" + sheepSelList.value.map((item) => item.PurId).toString()).then(res => {
            if (res.data > 0) {
                ElMessage({
                    type: "success",
                    message: "重新审核",
                    duration: 2000
                })
                handleShowPurchase();
            }
            else if (res.data == -1) {
                ElMessage({
                    type: "error",
                    message: "当前数据当中已有重新审核的数据",
                    duration: 2000
                })
                handleShowPurchase();
            }
            else {
                ElMessage({
                    type: "error",
                    message: "重新审核失败",
                    duration: 2000
                })
                handleShowPurchase();
            }
        })
    })
}
const handleDelete = (PurId: string) => {
    ElMessageBox.confirm(
        '确定要删除当前数据吗?',
        '警告',
        {
            confirmButtonText: '是',
            cancelButtonText: '否',
            type: 'warning',
        }
    ).then(() => {
        http.get("Organization/api/Subcultivation/DelSubcultivation?Id=" + PurId).then(res => {
            if (res.data > 0) {
                ElMessage({
                    type: "success",
                    message: "删除成功",
                    duration: 2000
                });
                UppType(PurId);
                handleShowPurchase();
            }
            else {
                ElMessage({
                    type: "error",
                    message: "删除失败",
                    duration: 2000
                });
                handleShowPurchase();
            }
        })
    })
}
const handleEdit = (PurId: PurList | any) => {
    addFormInfo();
    HYEHID.value = PurId.RaisedSheep;
    dialogTableVisible.value = true;
    // http.get("routechen/api/ReadPurchase/HandleGetPurchase?purId=" + PurId).then(res => {
    purchaseForm.SubcultivationId = PurId.SubcultivationId;
    purchaseForm.RaisedSheep = PurId.RaisedSheep;
    purchaseForm.ShiftCottage = PurId.ShiftCottage;
    purchaseForm.ShiftField = PurId.ShiftField;
    purchaseForm.AtPresentCottage = PurId.AtPresentCottage;
    purchaseForm.AtPresentField = PurId.AtPresentField;
    purchaseForm.Sex = PurId.Sex;
    purchaseForm.TransplantingAge = PurId.TransplantingAge;
    purchaseForm.ConversionDate = PurId.ConversionDate;



    GetField();
    GetField1();
    //     //rowList.value.push(newRow);

    // })
}

const SupplierList = ref<{}[]>([]);

const GetSupplier = () => {
    http.get("OrganizationRead/api/Supplier/SleectSupplier").then(res => {
        SupplierList.value = res.data;
    })
}
//关闭弹出层后执行
const NewUpdate = () => {

    rowList.value = [];
    dialogTableVisible.value = false;
}



//耳号
const Earnumber = ref([]);
//耳号
const GetEarnumber = () => {
    http.get("MYroute/api/SheepFile/SheepFileConditionEard").then(res => {
        Earnumber.value = res.data;
    })
}
const HYEHID = ref("");


const UppType = (EH: string) => {
    http.put("Mroute/api/SheepFile/SheepFileConditionUpdateprogeny?card=" + EH).then(res => {
        console.log(res.data)
    })
}

const HYList = ref([]);
const GetHYLidt = () => {
    http.get("MYroute/api/SheepFile/SheepFileConditionarDescendantfile").then(x => {
        console.log(x.data)
        HYList.value = x.data;
    })
}
const ChangData = () => {
    HYList.value.forEach(x => {
        if (x.earNumber == purchaseForm.RaisedSheep) {

            purchaseForm.AtPresentCottage = x.cottage;
            purchaseForm.AtPresentField = x.field;
            if (x.sex == 1) {
                purchaseForm.Sex = true;
            }
            else {
                purchaseForm.Sex = false;
            }
        }
    });
}


onMounted(() => {
    GetHYLidt();
    GetEarnumber();
    Getsheeptype();
    GetStashInfo();
    GetCooter();
    GetStaff();
    GetAccounting();
    GetDigest();
    handleShowPurchase();
    GetSupplier();
})

</script>